package com.husd.framework.autocode;

import com.husd.framework.autocode.dto.EbayWord;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;

/**
 * @author shengdonghu@126.com
 * @date 2023/4/23 10:02
 */
public class Ebay01SplitWordClassConvert {

    private AtomicLong idGen = new AtomicLong(0L);

    EbayWord pre = null;
    EbayWord next = null;
    List<EbayWord> wordList = new ArrayList<>();

    public List<EbayWord> split(String content) {

        // 先分词 把数据都标准化
        String str = content;
        char[] lineArr = str.toCharArray();
        int len = lineArr.length;
        int index = 0;
        for (; index < len; index++) {
            char c = lineArr[index];
            switch (c) {
                case ' ':
                case '\t':
                case '\n':
                    break;
                case '<':
                    int comment = isComment(lineArr, index);
                    if (comment > 0) {
                        //移动指针到注释结束的后面一个字符
                        index = comment + 1;
                    } else {
                        addWord(EbayWord.L1);
                    }
                    break;
                case '/':
                    addWord(EbayWord.XIE);
                    break;
                case '>':
                    addWord(EbayWord.R1);
                    break;
                case '=':
                    addWord(EbayWord.EQ);
                    break;
                case '"':
                    addWord(EbayWord.QUO);
                    break;
                default:
                    String sWord = findSimpleWord(lineArr, index);
                    // 如果有连续多个WORD ，那么要合并为一个
//                        EbayWord pre = wordList.get(wordList.size() - 1);
//                        if (pre != null && pre.getType() == EbayWord.EbayWorkType.WORD) {
//                            pre.setName(pre.getName() + " " + sWord);
//                        } else {
//                            addWord(new EbayWord(sWord, EbayWord.EbayWorkType.WORD));
//                        }
                    addWord(new EbayWord(sWord, EbayWord.EbayWorkType.WORD));
                    //走到这里就是普通字符 例如 abcde
                    index += sWord.length() - 1;
                    break;
            }
        }

        return wordList;
    }

    private EbayWord getPre(int index) {

        int preIndex = index - 1;
        if (preIndex < 0) {
            return null;
        }
        if (preIndex >= wordList.size()) {
            return null;
        }
        return wordList.get(preIndex);
    }

    private String findSimpleWord(char[] lineArr, int index) {

        // abc sdfsdf
        // 返回abc就对了
        int start = index;
        int end = index;
        int offset = 0;
        while (end < lineArr.length &&
                lineArr[end] != '=' &&
                lineArr[end] != '"' &&
                lineArr[end] != '>' &&
                lineArr[end] != '<' &&
                lineArr[end] != ' '
        ) {
            end++;
            offset++;
        }
        return new String(lineArr, start, offset);
    }

    private void addWord(EbayWord ebayWord) {

        long id = idGen.getAndAdd(1L);
        ebayWord.setId(id);
        wordList.add(ebayWord);
        // pre = ebayWord;
    }

    private int isComment(char[] lineArr, int index) {

        //  <!-- ... more Pictogram values allowed here ... -->
        // 当前index是<
        // <!--     -->
        if (StringUtils.equalsIgnoreCase("<!--", strFromCharArray(lineArr, index, 4))) {
            int start = index + 4;
            int wordStart = start;
            int offset = 0;
            while (!StringUtils.equalsIgnoreCase("-->",
                    strFromCharArray(lineArr, start, 3))) {
                start++;
                offset++;
            }
            // 走到这里，说明读取到了注释的内容，需要把注释的内容加入到分词的结果
            addWord(EbayWord.C1);
            String value = strFromCharArray(lineArr, wordStart, offset);
            addWord(new EbayWord(value, EbayWord.EbayWorkType.WORD));
            addWord(EbayWord.C2);
            return start + 2;
        } else {
            return 0;
        }
    }

    private String strFromCharArray(char[] lineArr, int offset, int count) {

        int len = lineArr.length;
        if (offset < 0 || offset >= len) {
            throw new RuntimeException("无效的offset: " + offset + " len:" + len);
        }
       // count = Math.max(count, (len - offset));
        return String.valueOf(lineArr, offset, count);
    }

    public static void main(String[] args) {

        Ebay01SplitWordClassConvert obj =
                new Ebay01SplitWordClassConvert();

        String str = "<!-- this is a test -->";
        char[] lineArr = str.toCharArray();
        int endIndex = obj.isComment(lineArr, 0);
        System.out.println(endIndex);

        String sWord = obj.findSimpleWord(lineArr, 5);
        System.out.println(sWord);
    }

}
